<template>
    <el-dialog
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      append-to-body
      width="35%"
      top="20px"
      style="height: 800px; overflow-y: hidden"
    >
      <el-input
        type="text"
        v-model="search_data"
        suffix-icon="el-icon-search"
        placeholder="请输入查询姓名"
        @keyup.enter.native="queryParticipant"
      >
      </el-input>
      <div class="BTN">
        <el-button
          icon="el-icon-close"
          v-for="(item, index) in addemsList"
          :key="item.id"
          @click="deleteems(index)"
          class="BTN"
        >
          {{item.name}}
        </el-button>
      </div>
      <div class="cards">
        <el-card
          v-for="(item, index) in emsList"
          :key="index"
          style="margin-top: 10px"
          :body-style="{width: '96%', display: 'flex', justifyContent: 'space-between', padding: '5px'}"
          @click.native=" addems(item)"
        >
          <div class="left">
            <el-avatar shape="square" :size="60" :src="item.header"></el-avatar>
          </div>
          <div class="right">
            <span class="right-left">
              {{item.name}}
              <p style="height: 5px"></p>
              {{item.address}}
            </span>
            <span class="right-right">
              职位：{{item.position.name}}
              <p style="height: 5px"></p>
              部门：{{item.department.name}}
            </span>
          </div>
        </el-card>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="emitToParentcel()">取消</el-button>
        <el-button type="primary" @click="emitToParentcfm()">确定</el-button>
      </span>
    </el-dialog>
</template>

<script>
import { copy } from '../../utils/common'
export default {
  name: 'AddParticipants',
  data() {
    return {
      search_data: '',
      dialogVisible: false,
      emsList: [], // 员工信息数组
      addemsList: [] // 新增的参与者数组
    }
  },
  mounted() {
    // this.getemsList()
  },
  methods: {
    // 获取员工数据传入emsList
    getemsList() {
      var page = 1
      var size = 10
      this.getRequest(`admin/emp/${page}/${size}`).then(resp => {
        if (resp) {
          this.emsList = resp.list
          console.log('this.emsList', this.emsList)
        }
      })
    },
    // 获取准确查询后的参与者信息
    queryParticipant() {
      var page = 1
      var size = 10
      var url = '/admin/emp/?currentPage=' + page + '&size=' + size
      if (this.search_pro) {
        url = url + '&name=' + this.search_data
        this.getRequest(url).then(resp => {
          this.emsList = resp.data
          // console.log(this.emsList)
        })
      } else {
        this.$message.error('请输入查询名字')
      }
    },
    // 显示参与者弹窗并初始化addemsList
    init(list) {
      this.dialogVisible = true
      this.addemsList = copy(list)
      this.getemsList()
    },
    // 将选中的参与者添加进addemsList
    addems(item) {
      var len = this.addemsList.length
      var count = 0
      for (var i = 0; i < len; i++) {
        if (this.addemsList[i].id === item.id && this.addemsList[i].name === item.name) break
        else count++
      }
      // 说明前面无相同数据
      if (count === len) {
        this.addemsList.push({
          id: item.id,
          name: item.name
        })
      }
      // console.log('添加addemsList:', this.addemsList)
    },
    // 取消参与，从addempList中移除
    deleteems(index) {
      this.addemsList.splice(index, 1)
    },
    // 确定添加
    emitToParentcfm() {
      this.dialogVisible = false
      this.$emit('child-event', this.addemsList)
    },
    // 取消添加
    emitToParentcel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
.cards {
  width: 96%;
  /* height: 400px; */
  height: 50vh;
  overflow-y: auto;
}
.cards::-webkit-scrollbar {
  display: none;
}
.el-card:hover {
  cursor: pointer;
  width: 97%;
  height: 72px;
  background-color: rgba(236,245,255);
}
.left {
  display: inline-block;
  width: 60px;
  height: 60px;
}
.right {
  display: inline-block;
  display: flex;
  width: 100%;
  height: 50px;
  padding: 5px;
  font-size: 18px;
}
.right-left {
  display: inline-block;
  width: 500px;
  height: 50px;
}
.right-right {
  display: inline-block;
  width: 100%;
  height: 50px;
  text-align: center;
}
.BTN {
  width: 100%;
  height: 10vh;
  overflow-y: auto;
}
.BTN .el-button {
  width: 6vw;
  height: 5vh;
  margin-top: 1vh;
  margin-left: 0px;
  margin-right: 10px;
}
</style>
